package br.great.functionalCore;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.XMLOutputter;
import br.great.beans.Photo;
import br.great.beans.User;


/**
* PhotoMap
* Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
*/

/**
 * @author Developpement of PhotoMap
 */
public class MakePhotoRSS {

/** method description
* @param var signification
* @exception exception class name
* @return return_value return signification
* @see package.class#member label
*/
private Element root;
private Element channel;
private Element item;
private Element title;
private Element link;
private Element media_t;
private Element media_c;

    public Element makePhotoRss(String nameCollection, Vector photos, User user){
        Namespace ns = Namespace.getNamespace("media", "http://search.yahoo.com/mrss");
        root = new Element("rss");
        Attribute version = new Attribute("version", "2.0");
        root.setAttribute(version);
/*  exemple for the output of rss
 * <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">
 * <channel>
 *   <title>A</title>
 *   <link>pl_images/A.jpg</link>
 *   <guid></guid>
 *   <media:thumbnail url="pl_thumbs/A_thumb.jpg" />
 *   <media:content url="pl_images/A.jpg" type="" />
 * </channel>
 * </rss>
 **/    
        channel = new Element("channel");
        for(int i=0; i<photos.size(); i++){
            Photo photo = (Photo)photos.get(i);
            String titlePhoto = photo.getName();
            String uriPhoto = "../photos/"+user.getLogin()+"/"+nameCollection+"/"+photo.getName()+".jpg";
//System.out.println(uriPhoto);
            item = new Element("item");

            title = new Element("title");
            title.setText(titlePhoto);

            link = new Element("link");
            link.setText(uriPhoto);
            Attribute urlthumb = new Attribute("url", uriPhoto);

            media_t = new Element("thumbnail");
            media_t.setAttribute(urlthumb);
            Attribute urlcontent = new Attribute("url", uriPhoto);

            media_c = new Element("content");
            media_c.setAttribute(urlcontent);

            media_t.setNamespace(ns);
            media_c.setNamespace(ns);

            item.addContent(title);
            item.addContent(link);
            item.addContent(media_t);
            item.addContent(media_c);
            channel.addContent(item);
        }
        root.addContent(channel);
        return root;
    }
    
    public boolean makeFileRSS(String chemin, String nameCollection,int idCollection, Vector photos, User user){
        try {
            Element element = makePhotoRss(nameCollection, photos, user);
            chemin += idCollection+".rss";
            XMLOutputter outputter = new XMLOutputter();
            outputter.output(element, new FileWriter(chemin));
        } catch (IOException ex) {
            Logger.getLogger(MakePhotoRSS.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
    
//    public static void main(String[] args){
//        MakePhotoRSS mpr = new MakePhotoRSS();
////        Element e = mpr.makePhotoRss("titleP", "uriPhoto");
////        XMLOutputter xo = new XMLOutputter();
////        System.out.println(xo.outputString(e));
//        
//        Vector v = new Vector();
//        Photo p = new Photo();
//        p.setName("abc");
//        p.setURIPhoto("http://dsddas.com");
//        v.add(p);
//        System.out.println(mpr.makeFileRSS("c:/abc.rss", v));
//    }
}
